﻿<%@ Page Title="" Language="C#" MasterPageFile="~/NestedMasterPageSimple.master" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      BindGVCart("query", string.Empty, 1);
    }
  }

  protected void BindGVCart(string method, string SmasCode, int quantity)
  {
    ShoppingCart userShoppingCart = Session["ShoppingCart"] as ShoppingCart;
    if (userShoppingCart != null)
    {
      switch (method)
      {
        case "remove":
          userShoppingCart.RemoveItem(SmasCode);
          break;
        case "update":
          userShoppingCart.UpdateItem(SmasCode, quantity);
          break;
      }
      GVCart.DataSource = userShoppingCart.CartItems;
      GVCart.DataBind();

      GVCart.Caption = "我的購物車 <span class='red-text'>(" + userShoppingCart.GetItemCount().ToString() + ")</span>";

      if (userShoppingCart.GetTotal() == 0)
      {
        lbl_SCTotal.Text = "小計   $ " + userShoppingCart.GetTotal().ToString();
        lbl_DISTotal.Visible = false;
        lbl_MilesTotal.Visible = false;
      }
      else
      {
        lbl_SCTotal.Text = "小計    " + userShoppingCart.GetTotal().ToString("$ #,###,###");
        string strOrderList = JsonConvert.SerializeObject(userShoppingCart.CartItems).Replace("Quantity", "QTY");
        B2C_MALL_ORDER ws = new B2C_MALL_ORDER();
        ws.Url = ConfigurationManager.AppSettings["B2C_MALL_ORDER"].ToString(); 
        string strJson = ws.B2C_MALL_ValidateOrderInfo(CommonUtil.GetWebDevice()
          , HttpContext.Current.User.Identity.Name
          , "3", "3", "1"
          , strOrderList
          , "", "", "", "", "", "", "2", "", "", "", User.Identity.Name);
        JObject jObj = JsonConvert.DeserializeObject<JObject>(strJson);
        if (jObj["Is_Valid"].ToString() == "Y")
        {
          var ja = jObj["pOrderSumList_Ary"];
          //單價總計(含稅)=已折扣後金額+折扣金額
          //decActPrice = Convert.ToDecimal(ja["PriceSum"]) + Convert.ToDecimal(ja["discountSum"]);
          //decFinalPrice = Convert.ToDecimal(ja["PriceSum"]);
          string mile_Sum = string.IsNullOrEmpty(ja["mile_Sum"].ToString()) ? "0" : ja["mile_Sum"].ToString();
          lbl_MilesTotal.Text = "抵扣哩程：" + ja["mile_Sum"].ToString();
          lbl_MilesTotal.Visible = true;
          lbl_DISTotal.Text = "優惠價 $ " + Convert.ToDecimal(ja["PriceSum"]).ToString("n0");
          lbl_DISTotal.Visible = true;
        }
      }

      if (userShoppingCart.GetItemCount() > 0)
      {
        lb_ConfirmOrder.Visible = true;
      }
      else
      {
        lb_ConfirmOrder.Visible = false;
      }
      //更新Master Page中的筆數
      Label myCartCounter = Master.Master.FindControl("myCartCounter") as Label;
      myCartCounter.Text = userShoppingCart.GetItemCount().ToString();
    }
    else
    {
      GVCart.DataBind();
      GVCart.Caption = "我的購物車 <span class='red-text'>(0)</span>";
      lbl_SCTotal.Text = "小計   $ 0";
      lb_ConfirmOrder.Visible = false;
    }
  }

  protected void GVCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    Label lbl_SMAS_CODE = GVCart.Rows[e.RowIndex].FindControl("lbl_SMAS_CODE") as Label;
    BindGVCart("remove", lbl_SMAS_CODE.Text, 1);
  }

  protected void GVCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
    Label lbl_SMAS_CODE = GVCart.Rows[e.RowIndex].FindControl("lbl_SMAS_CODE") as Label;
    TextBox txtboxQty = GVCart.Rows[e.RowIndex].FindControl("txtboxQty") as TextBox;
    if (!string.IsNullOrEmpty(txtboxQty.Text))
    {
      if (txtboxQty.Text != "0")
      {
        BindGVCart("update", lbl_SMAS_CODE.Text, int.Parse(txtboxQty.Text));
      }
      else
      {
        BindGVCart("update", lbl_SMAS_CODE.Text, 1);
      }
    }
    else
    {
      BindGVCart("update", lbl_SMAS_CODE.Text, 1);
      txtboxQty.Text = "1";
    }

  }

  protected void GVCart_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      Label lbl_ProdCatID = e.Row.FindControl("lbl_ProdCatID") as Label;
      Label lbl_B2C_CODE = e.Row.FindControl("lbl_B2C_CODE") as Label;

      ImageButton img = e.Row.FindControl("img_Path") as ImageButton;
      img.PostBackUrl = "~/Products/ProductDetail.aspx?B2C_CODE=" + lbl_B2C_CODE.Text;

      LinkButton lb_ProductName = e.Row.FindControl("lb_ProductName") as LinkButton;
      lb_ProductName.PostBackUrl = "~/Products/ProductDetail.aspx?B2C_CODE=" + lbl_B2C_CODE.Text;
    }
  }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="Server">
  <div class="list-table wish">
    <asp:GridView ID="GVCart" runat="server" AutoGenerateColumns="False" GridLines="None" EmptyDataText="目前購物車中沒有任何商品項目!" OnRowDeleting="GVCart_RowDeleting" OnRowUpdating="GVCart_RowUpdating" OnRowDataBound="GVCart_RowDataBound">
      <Columns>
        <asp:BoundField HeaderStyle-CssClass="spacer">
          <HeaderStyle CssClass="spacer"></HeaderStyle>
        </asp:BoundField>
        <asp:TemplateField HeaderText="&lt;span class='select-all'&gt;全 選&lt;/span&gt;" Visible="False">
          <ItemTemplate>
            <asp:CheckBox ID="cb_item" runat="server" />
          </ItemTemplate>
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="t-a-c" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="商品圖">
          <ItemTemplate>
            <asp:ImageButton ID="img_Path" runat="server" Width="50px" ImageUrl='<%# Eval("ImagePath") %>' />
            <asp:Label ID="lbl_ItemId" runat="server" Text='<%# Server.HtmlEncode(Eval("ItemId").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_SMAS_CODE" runat="server" Text='<%# Server.HtmlEncode(Eval("SMAS_CODE").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_B2C_CODE" runat="server" Text='<%# Server.HtmlEncode(Eval("B2C_CODE").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_ProdCatID" runat="server" Text='<%# Server.HtmlEncode(Eval("ProdCatID").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_StorageQty" runat="server" Text='<%# Server.HtmlEncode(Eval("StorageQty").ToString()) %>' Visible="false" />
          </ItemTemplate>
          <ItemStyle Width="50px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="商品名稱">
          <ItemTemplate>
            <asp:LinkButton ID="lb_ProductName" runat="server" Text='<%# Server.HtmlEncode(Eval("ProductName").ToString()) %>'></asp:LinkButton>            
            <%# ((dynamic)Eval("Promotion")).ToString() %>
          </ItemTemplate>
          <ItemStyle Width="230px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="金額">
          <ItemTemplate>
            <asp:Label ID="lbl_Price" runat="server" Text='<%# Server.HtmlEncode(Eval("Price", "{0:$ #,###,###}").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="數量">
          <ItemTemplate>
            <asp:TextBox ID="txtboxQty" runat="server" Text='<%# Bind("Quantity") %>' MaxLength="3"></asp:TextBox>
            <asp:LinkButton ID="btnSave" runat="server" CausesValidation="True" CommandName="Update" Text="儲存修改" CssClass="save"></asp:LinkButton>
          </ItemTemplate>
          <ItemStyle Width="150px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="抵購哩程">
          <ItemTemplate>
            <asp:Label ID="lbl_Mileage" runat="server" Text='<%# Server.HtmlEncode(Eval("Mileage").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="規格">
          <ItemTemplate>
            <asp:Label ID="lbl_Spec" runat="server" Text='<%# Server.HtmlEncode(Eval("Spec").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="商品狀態" Visible="False">
          <ItemTemplate>
            <asp:Label ID="lbl_Status" runat="server" Text='<%# Server.HtmlEncode(Eval("Status").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/Icons/Delete.png" ShowDeleteButton="True" />
      </Columns>
    </asp:GridView>
    <div class="pink-result">
      <asp:Label ID="lbl_SCTotal" runat="server" />
      <br />
      <asp:Label ID="lbl_MilesTotal" runat="server" />
      <br />
      <asp:Label ID="lbl_DISTotal" runat="server" />
    </div>
  </div>
  <div class="control-links">
    <asp:LinkButton ID="lb_ConfirmOrder" runat="server"
      CssClass="pink-btn fl" PostBackUrl="~/Orders/OrderSummaryAndAmount.aspx">
      <img id="CheckoutImg" src="~/Images/Icons/Checkout.png" runat="server" />確定結帳
    </asp:LinkButton>
    <%--<a href="#" class="pink-btn fl">
      <img src="../Images/Icons/Checkout.png" />
      確定結帳</a>--%>
    <asp:LinkButton ID="lb_Back" runat="server" Text="返回繼續選購"
      CssClass="btn fr" PostBackUrl="~/Home.aspx">返回繼續選購</asp:LinkButton>
  </div>

</asp:Content>

